import heapq
class Solution(object):
    def minCostConnectPoints(self, points):
        n = len(points)
        p = [0] * n
        p[0] = 1
        st = []
        for i in range(1, n):
            st.append([abs(points[i][1] - points[0][1]) + abs(points[i][0] - points[0][0]), i])
            heapq.heapify(st)
        ans = 0
        while st:
            while st and p[st[0][1]]:
                heapq.heappop(st)
            if st:
                dist, u = heapq.heappop(st)
                ans += dist
                p[u] = 1
                for i in range(n):
                    if not p[i]:
                        heapq.heappush(st, [abs(points[i][1] - points[u][1]) + abs(points[i][0] - points[u][0]), i])
        return ans
